import { ElMessage, ElMessageBox } from "element-plus";
import Typings from "../typings";

/**
 * 消息提示队列
 * ElMessage 自动关闭前的时间内，重复的错误提示只显示一次
 */
let msgArr: string[] = [];

export const $singleMsg: (options: Typings.Message.option) => void = (
  options = {
    message: "消息",
  }
) => {
  if (!msgArr.some((item) => item === options.message)) {
    msgArr.push(options.message);
    ElMessage({
      type: "success",
      ...options,
      onClose: () => {
        msgArr = msgArr.filter((item: string) => item !== options.message);
      },
    });
  }
};

/**
 * confirm 确认弹窗
 */
export const $confirm = async ({
  tip,
  title = "温馨提示",
  type = "warning",
  sureText = "确定",
  cancelText = "取消",
}: Typings.MessageBox.option) => {
  await ElMessageBox.confirm(tip, title, {
    confirmButtonText: sureText,
    cancelButtonText: cancelText,
    type,
  });
};

/**
 * element - alert
 * 对于同样的信息不会触发两次
 */
let alertMsgQueue: string[] = [];
export const $alert = async ({
  tip,
  title = "温馨提示",
  type = "warning",
  sureText = "确定",
  cancelText = "取消",
  callback,
}: Typings.MessageBox.option) => {
  if (!alertMsgQueue.some((item) => item === tip)) {
    alertMsgQueue.push(tip);
    await ElMessageBox.alert(tip, title, {
      confirmButtonText: sureText,
      cancelButtonText: cancelText,
      type,
      callback: () => {
        alertMsgQueue = alertMsgQueue.filter((item: string) => item !== tip);
        callback && callback();
      },
    });
  } else {
    return Promise.reject();
  }
};
